/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/


var Scroller = function(containerId, leftArrowId, rightArrowId) {
	this.container = document.getElementById(containerId);
	this.leftArrow = get(leftArrowId);
	this.rightArrow = get(rightArrowId);
	
	this.leftArrowVisible = false;
	this.rightArrowVisible = false;
	
	var self = this;
	$(this.container).scroll(function(){
		self.update();
	});
}

Scroller.prototype.update = function () {
	if (this.container.scrollLeft + this.container.offsetWidth >= this.container.scrollWidth) {
		if(this.rightArrowVisible) {
			this.rightArrow.removeClass("selected");
			this.rightArrowVisible = false;
		}
	} else {
		if(!this.rightArrowVisible) {
			this.rightArrow.addClass("selected");
			this.rightArrowVisible = true;
		}	
	}
	
	if(this.container.scrollLeft <= 0) {
		if(this.leftArrowVisible) {
			this.leftArrow.removeClass("selected");
			this.leftArrowVisible = false;
		}
	} else {
		if(!this.leftArrowVisible) {
			this.leftArrow.addClass("selected");
			this.leftArrowVisible = true;
		}
	}
}

Scroller.itemScrollIntoView = function(obj, scroller, speed, callback){
	if (speed == 0) {
		if (callback) 
			callback();
		return;		
	}
	
	if (scroller.offsetWidth >= scroller.scrollWidth) {
		if (callback) 
			callback();
		return;
	}
	
	var offset = Math.round(scroller.offsetWidth / 2 - obj.offsetWidth / 2);
	if (obj.offsetLeft - offset < scroller.scrollLeft) {
		var targetOffset = obj.offsetLeft - offset;
		if (targetOffset < 0) 
			targetOffset = 0;	
		
		if (targetOffset != scroller.scrollLeft) {
			$(scroller).stop();
			$(scroller).animate({
				scrollLeft: targetOffset
			}, speed, function(){
				if(callback) callback();
			});
			return;
		}
	}
	
	if (obj.offsetLeft + obj.offsetWidth + offset > scroller.scrollLeft + scroller.offsetWidth) {
		var targetOffset = obj.offsetLeft - scroller.offsetWidth + obj.offsetWidth + offset;
		if (targetOffset + scroller.offsetWidth > scroller.scrollWidth) 
			targetOffset = scroller.scrollWidth - scroller.offsetWidth;
		
		if (targetOffset != scroller.scrollLeft) {
			$(scroller).stop();
			$(scroller).animate({
				scrollLeft: targetOffset
			}, speed, function(){
				if (callback) callback();
			});
			return;
		}
	}
	
	if (callback) 
		callback();
}

Scroller.itemScrollIntoViewWithoutAnimation = function(obj, scroller, callback){
	
	if (scroller.offsetWidth >= scroller.scrollWidth) {
		if (callback) 
			callback();
		return;
	}
	
	var offset = Math.round(scroller.offsetWidth / 2 - obj.offsetWidth / 2);
	if (obj.offsetLeft - offset < scroller.scrollLeft) {
		var targetOffset = obj.offsetLeft - offset;
		if (targetOffset < 0) 
			targetOffset = 0;	
		
		if (targetOffset != scroller.scrollLeft) {			
			$(scroller).scrollLeft(targetOffset);
			if(callback) callback();
			return;
		}
	}
	
	if (obj.offsetLeft + obj.offsetWidth + offset > scroller.scrollLeft + scroller.offsetWidth) {
		var targetOffset = obj.offsetLeft - scroller.offsetWidth + obj.offsetWidth + offset;
		if (targetOffset + scroller.offsetWidth > scroller.scrollWidth) 
			targetOffset = scroller.scrollWidth - scroller.offsetWidth;
		
		if (targetOffset != scroller.scrollLeft) {
			$(scroller).scrollLeft(targetOffset);
			if (callback) callback();
			return;
		}
	}
	
	if (callback) 
		callback();
}

Scroller.itemScrollIntoViewVertical = function(obj, scroller, speed, callback){
	if (speed == 0) {
		if (callback) 
			callback();
		return;		
	}

	if(scroller.offsetHeight >= scroller.scrollHeight){
		if(callback) callback();
		return;
	} 
	
	var offset = Math.round(scroller.offsetHeight/2 - obj.offsetHeight/2);
	
	if (obj.offsetTop - offset < scroller.scrollTop) {
		var targetOffset = obj.offsetTop - offset;
		if(targetOffset < 0) targetOffset = 0;
		
		if (targetOffset != scroller.scrollTop) {
			$(scroller).stop();
			$(scroller).animate({
				scrollTop: targetOffset
			}, speed, callback);
			return;
		}
	}
	
	if (obj.offsetTop + obj.offsetHeight + offset > scroller.scrollTop + scroller.offsetHeight) {
		var targetOffset = obj.offsetTop - scroller.offsetHeight + obj.offsetHeight + offset;
		
		if(targetOffset > obj.offsetTop) {
			targetOffset = obj.offsetTop - offset;
		}
		
		if(targetOffset + scroller.offsetHeight > scroller.scrollHeight) 
			targetOffset = scroller.scrollHeight - scroller.offsetHeight;
			
		if (targetOffset != scroller.scrollTop) {
			$(scroller).stop();
			$(scroller).animate({
				scrollTop: targetOffset
			}, speed, callback);
			return;
		}
	}
	
	if(callback) callback();
}